package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OmcFoodDto;
import com.csun.cmny.provider.pojo.OmcFood;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcFoodService;
import com.csun.cmny.provider.utils.ValidateUtil;
import com.csun.cmny.provider.validator.OmcFoodValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 食物管理控制器
 */
@RestController
@RequestMapping(value = "/food", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcFoodController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcFoodService omcFoodService;


    /**
     * 添加食物
     */
    @ValidateAnnotation
    @RequestMapping(value = "/foodAdd", method = RequestMethod.POST)
    public Wrapper foodAdd(@Validated(value = {OmcFoodValidator.class}) @RequestBody OmcFoodDto omcFoodDto, BindingResult br) {
        omcFoodDto.setGroupId(getLoginAuthDto().getGroupId());
        //验证需修改的食物名是否已存在
        OmcFood omcFood = new OmcFood();
        omcFood.setFoodName(omcFoodDto.getFoodName());
        omcFood.setGroupId(getLoginAuthDto().getGroupId());
        int m = omcFoodService.selectCount(omcFood);
        if (m > 0) {//需添加的食物已存在,不能重复添加.
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("food.exist.error", null), m);
        }
        int n = saveOmcFood(omcFoodDto);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.insert.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.insert.success", null), n);
    }

    /**
     * 修改食物
     *
     * @param omcFoodDto
     * @param br
     * @return
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Wrapper updateById(@RequestBody OmcFoodDto omcFoodDto, BindingResult br) {
        //验证参数
        if (null == omcFoodDto.getId() || omcFoodDto.getId() <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        String[] validateParam = {"foodId", "foodName", "price", "picture"};
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, validateParam);
        if (errorMap != null && errorMap.size() != 0) {
            /* 返回参数校验错误详情 */
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("food.eidt.param.error ", null), errorMap);
        }
        omcFoodDto.setGroupId(getLoginAuthDto().getGroupId());
        //验证需修改的食物名是否已存在
        Example example = new Example(OmcFood.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("id", omcFoodDto.getId());
        criteria.andEqualTo("groupId", omcFoodDto.getGroupId());
        criteria.andEqualTo("foodName", omcFoodDto.getFoodName());
        int m = omcFoodService.selectCountByExample(example);
        if (m <= 0) {//需修改的食物名不存在可以修改
            int n = saveOmcFood(omcFoodDto);
            if (n <= 0) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.update.fail", null), n);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.update.success", null), n);
        }
        return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("food.exist.error", null), m);
    }


    /**
     * 通过食物ID查询食物
     */
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(Integer id) {
        //验证参数
        if (null == id || id <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        OmcFood omcFood = omcFoodService.selectByKey(id);
        if (null == omcFood) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.query.fail", null), omcFood);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), omcFood);
    }

    /**
     * 通过食物ID删除食物
     */
    @RequestMapping(value = "/foodDelById", method = RequestMethod.POST)
    public Wrapper foodDelById(Integer id) {
        //验证参数
        if (null == id || id <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        int n = omcFoodService.deleteByKey(id);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.delete.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.delete.sucess", null), n);
    }

    /**
     * 查询出所有的食物
     */
    @RequestMapping(value = "/selectShowAll", method = RequestMethod.POST)
    public Wrapper selectShowAll() {
        OmcFood omcFood = new OmcFood();
        omcFood.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcFood> list = omcFoodService.select(omcFood);
        if (list.size() <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.query.fail", null), list);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }

    /**
     * 通过食物类型查询出某一类的所有食物
     */
    @RequestMapping(value = "/selectByType", method = RequestMethod.POST)
    public Wrapper selectByType(Integer foodType) {
        if (null == foodType || foodType < 1 || foodType > 4) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Food.type.range.error", null), null);
        }
        OmcFood omcFood = new OmcFood();
        omcFood.setGroupId(getLoginAuthDto().getGroupId());
        omcFood.setFoodType(foodType);
        List<OmcFood> list = omcFoodService.select(omcFood);
        if (list.size() <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.delete.fail", null), list);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.delete.sucess", null), list);
    }

    /**
     * 保存或者更新食物信息
     *
     * @param: [uacPlanDto]
     * @return: java.lang.Integer
     */
    private Integer saveOmcFood(OmcFoodDto omcFoodDto) {
        OmcFood omcFood = new OmcFood();
        BeanUtils.copyProperties(omcFoodDto, omcFood);
        return omcFoodService.saveOmcFood(omcFood, getLoginAuthDto());
    }


}
